define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bdl/bd/Materialimage/index' + location.search,
                    multi_url: 'bdl/bd/Materialimage/multi',
                    export_url: 'bdl/bd/Materialimage/export',
                    add_url: 'bdl/bd/Materialimage/add',
                    edit_url: 'bdl/bd/Materialimage/edit',
                    del_url: 'bdl/bd/Materialimage/del',
                    table: 'bd_materialImage',
                }
            });

            var table       = $("#table");
            var showExport  = $(".btn-export").data('value')

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                showToggle: false,
                showExport: showExport,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID',operate:false},
                        {
                            field: 'forgid', title: __('数据中心/组织'), searchList: function (column) {
                                return Template('forgidtpl', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fnumber', title: '编码', operate: 'LIKE'},
                        {field: 'fname', title: '名称', operate: 'LIKE'},
                        {field: 'fk_bdl_imageurl', title: '图片', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'fk_bdl_bd_material', title: '产品ID', operate: false},
                        {field: 'material.fname', title: '产品名称', operate: 'LIKE'},
                        {field: 'material.fnumber', title: '产品编码', operate: 'LIKE'},
                        {field: 'fk_bdl_bd_surface', title: '表面ID', operate: false},
                        {field: 'surface.fname', title: '表面名称', operate: 'LIKE'},
                        {field: 'surface.fnumber', title: '表面编码', operate: 'LIKE'},


                        {field: 'datacenter.fname', title: '数据中心名称',operate: false},
                        {field: 'forg.fname', title: '组织名称',operate: false},
                        {field: 'forg.fnumber', title: '组织编码',operate: false},
                        {field: 'forg.id', title: '组织ID',operate: false},

                        {field: 'fk_bdl_comment', title: '备注', operate: 'LIKE'},
                        {field: 'fk_bdl_creatorid', title: '创建人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'creator.nickname', title: '创建人', operate: false},
                        {field: 'fk_bdl_createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_fmodifierid', title: '修改人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'modifier.nickname', title: '修改人', operate: false},
                        {field: 'fk_bdl_modifytime', title: '修改时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function ()
        {
            $(document).ready(function(){
                $(document).on('change','#forgid',function(){
                    var that = $(this)
                    var val  = that.val()
                    layer.load(1);

                    $.ajax({
                        url:'bdl/bd/datacenter/getFilter?type=get_fk_bdl_bd_surface_list_for_form_select',
                        data:{forgid:val},
                        success:function (res)
                        {
                            if(res.code == 1)
                            {
                                var str = `<select  id="c-fk_bdl_bd_surface" required class="form-control selectpicker"  data-live-search="true"  name="row[fk_bdl_bd_surface]">`


                                for (i=0;i<res.data.length;i++)
                                {
                                    str += `<option value="${res.data[i]['value']}" >${res.data[i]['name']}</option>`
                                }
                                str +=  `</select>`

                                $(".fk_bdl_bd_surface").html(str)

                                Form.api.bindevent($(".fk_bdl_bd_surface"));
                            }else{
                                layer.alert(res.msg)
                            }

                            layer.closeAll('loading');

                        }
                    })


                })
            })

            Controller.api.bindevent();
        },
        edit: function ()
        {
            $(document).ready(function()
            {
                // 初始化计数器
                let selectChangeCount = 0;


                $(document).on('change','#forgid',function()
                {
                    selectChangeCount++;

                    var that = $(this)
                    var val  = that.val()

                    // 客户
                    var cust      = $("#sur").val()

                    if(val !== '')
                    {
                        if(selectChangeCount > 1 || cust == 2)
                        {

                            layer.load(1);

                            $.ajax({
                                url:'bdl/bd/datacenter/getFilter?type=get_fk_bdl_bd_surface_list_for_form_select',
                                data:{forgid:val},
                                success:function (res)
                                {
                                    if(res.code == 1)
                                    {
                                        var str = `<select  id="c-fk_bdl_bd_surface" required class="form-control selectpicker"  data-live-search="true"  name="row[fk_bdl_bd_surface]">`


                                        for (i=0;i<res.data.length;i++)
                                        {
                                            str += `<option value="${res.data[i]['value']}" >${res.data[i]['name']}</option>`
                                        }
                                        str +=  `</select>`

                                        $(".fk_bdl_bd_surface").html(str)

                                        Form.api.bindevent($(".fk_bdl_bd_surface"));
                                    }else{
                                        layer.alert(res.msg)
                                    }

                                    layer.closeAll('loading');

                                }
                            })

                        }
                    }




                })
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});
